<style lang="less">
@import '../../../styles/table-common.less';
@import '../../personInfo.less';
@import './quartzManage.less';
</style>
<style lang="less" scoped>
.icon01 {
    background: rgb(45, 154, 244); border-radius: 50%; padding: 3px
}
.cmr16{
    margin-right: 16px
}

.cw250{
  width: 250px
}

.cmb5 {
    margin-bottom: 5px
}
.marin01 {
    margin: 0 3px 3px 0
}
</style>
<template>
    <div class="search">
        <Row :gutter="16" class="all-height">
            <Col :span="showRight ? 18 : 24" class="all-height">
                <Card class="all-height card-child-height">
                    <Row class="operation code-row-bg" style="display: flex; justify-content: space-between">
                        <Col span="22">
                            <Button @click="addRole" type="primary" icon="md-add">添加新任务</Button>
                            <Button @click="delAll" icon="md-trash">批量删除</Button>
                            <Button @click="init" icon="md-refresh">刷新</Button>
                            <Button type="dashed" @click="openTip = !openTip">{{ openTip ? '关闭提示' : '开启提示' }}</Button>
                        </Col>
                        <Col span="2" style="text-align: right">
                            <Icon
                                color="#fff"
                                class="icon01"
                                :type="showRight ? `ios-arrow-forward` : `ios-arrow-back`"
                                @click.native="clickShowRight"
                            ></Icon>

                            <!--              <Button :icon="showRight?`ios-arrow-forward`:`ios-arrow-back`"-->
                            <!--                      @click.native="clickShowRight"-->
                            <!--                      type="primary">-->
                            <!--              </Button>-->
                        </Col>
                    </Row>
                    <Row v-show="openTip">
                        <Alert show-icon>
                            已选择
                            <span class="select-count">{{ selectList.length }}</span>
                            项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row class="calc-px-height">
                        <Table
                            :height="height1"
                            :loading="loading"
                            border
                            :columns="columns"
                            :data="data"
                            ref="table"
                            sortable="custom"
                            @on-sort-change="changeSort"
                            @on-selection-change="changeSelect"
                            @on-row-dblclick="sjclaib"
                        ></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page
                            :current="searchForm.pageNumber"
                            :total="total"
                            :page-size="searchForm.pageSize"
                            @on-change="changePage"
                            @on-page-size-change="changePageSize"
                            :page-size-opts="[10, 20, 50, 100, 200]"
                            size="small"
                            show-total
                            show-elevator
                            show-sizer
                        ></Page>
                    </Row>
                </Card>
            </Col>
            <Col span="6" class="all-height" v-if="showRight">
                <Card class="all-height card-child-height">
                    <Row v-show="!modalVisible" style="flex-direction: column">
                        <div class="drawer-header">
                            <div class="cmr16">查询区</div>
                        </div>
                        <Input v-model="searchForm.key" suffix="ios-search" @on-change="getDataList" placeholder="输入关键词搜索" clearable class="cw250" />
                    </Row>
                    <Row class="calc-sm-height" v-show="modalVisible" style="flex-direction: column">
                        <div class="drawer-header">
                            <div class="cmr16">{{ modalTitle }}</div>
                        </div>
                        <Form ref="form" :model="form" :label-width="labelWidth100" :rules="formValidate">
                            <FormItem label="任务类引用路径" prop="jobClassName">
                                <Input v-model="form.jobClassName" placeholder="例如 cn.exrick.xboot.quartz.jobs.Job" clearable />
                            </FormItem>
                            <FormItem label="cron表达式" prop="cronExpression" class="cmb5">
                                <Input v-model="form.cronExpression" clearable />
                                <a target="_blank" href="https://www.pppet.net/">
                                    <Icon type="md-arrow-dropright-circle" size="16" class="marin01" />
                                    在线cron表达式生成
                                </a>
                            </FormItem>
                            <FormItem label="参数" prop="parameter">
                                <Input v-model="form.parameter" />
                            </FormItem>
                            <FormItem label="备注" prop="description">
                                <Input v-model="form.description" />
                            </FormItem>
                        </Form>
                        <div class="br right-bottom-edit">
                            <Button type="text" @click="cancelSubmit">取消</Button>
                            <Button type="primary" :loading="submitLoading" @click="handleSubmit">保存并执行</Button>
                        </div>
                    </Row>
                </Card>
            </Col>
        </Row>
        <!-- <Card>


    </Card> -->

        <!--    <Modal-->
        <!--      draggable-->
        <!--      :title="modalTitle"-->
        <!--      v-model="modalVisible"-->
        <!--      :mask-closable="false"-->
        <!--      :width="500"-->
        <!--    >-->

        <!--    </Modal>-->
    </div>
</template>

<script>
import { getQuartzListData, addQuartz, editQuartz, pauseQuartz, resumeQuartz, deleteQuartz, doOnce } from '@/api/index';
export default {
    name: 'quartz-manage',
    data() {
        return {
            labelWidth100: 100,
            openTip: true,
            loading: true,
            sortColumn: 'createTime',
            sortType: 'desc',
            modalType: 0,
            modalVisible: false,
            modalTitle: '',
            height1: 0,
            searchForm: {
                // 搜索框初始化对象
                pageNumber: 1, // 当前页数
                pageSize: 50, // 页面大小
                sort: 'createTime', // 默认排序字段
                order: 'desc', // 默认排序方式
                key: ''
            },
            form: {
                id: '',
                jobClassName: '',
                cronExpression: '',
                paramter: '',
                description: ''
            },
            formValidate: {
                jobClassName: [{ required: true, message: '任务类名不能为空', trigger: 'change' }],
                cronExpression: [{ required: true, message: 'cron表达式不能为空', trigger: 'change' }]
            },
            submitLoading: false,
            selectList: [],
            columns: [],
            columns_1: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    type: 'index',
                    title: '序号',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '任务类',
                    key: 'jobClassName',
                    sortable: true,
                    minWidth: 300
                },
                {
                    title: 'cron表达式',
                    key: 'cronExpression',
                    sortable: true,
                    width: 150
                },
                {
                    title: '参数',
                    key: 'parameter',
                    sortable: true,
                    minWidth: 180
                },
                {
                    title: '备注',
                    key: 'description',
                    sortable: true,
                    minWidth: 180
                },
                {
                    title: '状态',
                    key: 'status',
                    align: 'center',
                    width: 130,
                    render: (h, params) => {
                        let re = '';
                        if (params.row.status == 0) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'success',
                                        text: '执行中'
                                    }
                                })
                            ]);
                        } else if (params.row.status == -1) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'error',
                                        text: '已停止'
                                    }
                                })
                            ]);
                        }
                    },
                    filters: [
                        {
                            label: '执行中',
                            value: 0
                        },
                        {
                            label: '已停止',
                            value: -1
                        }
                    ],
                    filterMultiple: false,
                    filterMethod(value, row) {
                        if (value == 0) {
                            return row.status == 0;
                        } else if (value == -1) {
                            return row.status == -1;
                        }
                    }
                },
                {
                    title: '创建时间',
                    key: 'createTime',
                    width: 170,
                    sortable: true,
                    sortType: 'desc'
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    fixed: 'right',
                    width: 220,
                    render: (h, params) => {
                        let runOrResume = '';
                        if (params.row.status == 0) {
                            runOrResume = h(
                                'a',
                                {
                                    on: {
                                        click: () => {
                                            this.pause(params.row);
                                        }
                                    }
                                },
                                '暂停'
                            );
                        } else {
                            runOrResume = h(
                                'a',
                                {
                                    on: {
                                        click: () => {
                                            this.resume(params.row);
                                        }
                                    }
                                },
                                '恢复执行'
                            );
                        }
                        return h('div', [
                            runOrResume,
                            h('Divider', {
                                props: {
                                    type: 'vertical'
                                }
                            }),
                            h(
                                'a',
                                {
                                    on: {
                                        click: () => {
                                            this.edit(params.row);
                                        }
                                    }
                                },
                                '编辑'
                            ),
                            h('Divider', {
                                props: {
                                    type: 'vertical'
                                }
                            }),
                            h(
                                'a',
                                {
                                    on: {
                                        click: () => {
                                            this.doOnce(params.row);
                                        }
                                    }
                                },
                                '执行一次'
                            ),
                            h('Divider', {
                                props: {
                                    type: 'vertical'
                                }
                            }),
                            h(
                                'a',
                                {
                                    on: {
                                        click: () => {
                                            this.remove(params.row);
                                        }
                                    }
                                },
                                '删除'
                            )
                        ]);
                    }
                }
            ],
            data: [],
            total: 0,
            showRight: true
        };
    },
    methods: {
        tableheig() {
            var bbb = document.getElementsByClassName('calc-px-height')[0].scrollHeight;
            var aaa = document.getElementsByClassName('calc-px-height')[0].children[0].children[0].children[1];
            var ccc = document.getElementsByClassName('calc-px-height')[0].children[0].children[0].children[0].scrollHeight;
            var ddd = bbb - ccc;
            console.log(bbb, '00000000000000');
            aaa.style.height = `${ddd}px`;

            if (this.searchForm.pageSize === 10) {
                this.height1 = 0;
            } else {
                this.height1 = document.getElementsByClassName('calc-px-height')[0].scrollHeight;
                if (this.total < this.searchForm.pageNumber * this.searchForm.pageSize) {
                    var r = (this.searchForm.pageNumber - 1) * this.searchForm.pageSize;
                    var a = this.total - r;
                    var s = a * 43 + 65;
                    if (s < this.height1) {
                        this.height1 = 0;
                    }
                }
            }
            if (this.total < 10) {
                this.height1 = 0;
            }
        },
        init() {
            this.getDataList();
        },
        // 显示隐藏右侧区域
        clickShowRight() {
            this.showRight = !this.showRight;
        },
        changePage(v) {
            this.searchForm.pageNumber = v;
            this.getDataList();
            this.clearSelectAll();
        },
        changePageSize(v) {
            this.searchForm.pageSize = v;
            this.getDataList();
        },
        changeSort(e) {
            this.searchForm.sort = e.key;
            this.searchForm.order = e.order;
            if (e.order == 'normal') {
                this.searchForm.order = '';
            }
            this.getDataList();
        },
        getDataList() {
            this.loading = true;
            getQuartzListData(this.searchForm).then(res => {
                this.loading = false;
                if (res.success) {
                    this.data = res.result.content;
                    this.total = res.result.totalElements;
                    this.tableheig();
                    if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
                        this.searchForm.pageNumber -= 1;
                        this.getDataList();
                    }
                }
            });
        },
        cancelSubmit() {
            this.modalVisible = false;
        },
        handleSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (this.modalType == 0) {
                        // 添加
                        this.submitLoading = true;
                        addQuartz(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.getDataList();
                                this.modalVisible = false;
                            }
                        });
                    } else {
                        this.submitLoading = true;
                        editQuartz(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.getDataList();
                                this.modalVisible = false;
                            }
                        });
                    }
                }
            });
        },
        addRole() {
            this.modalType = 0;
            this.modalTitle = '添加任务';
            this.$refs.form.resetFields();
            (this.form = {
                paramter: '',
                description: ''
            }),
                (this.modalVisible = true);
        },
        // 双击表格行
        sjclaib(v) {
            this.showRight = true;
            this.modalType = 1;
            this.modalTitle = '编辑任务';
            this.$refs.form.resetFields();
            // 转换null为""
            for (let attr in v) {
                if (v[attr] == null) {
                    v[attr] = '';
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);
            this.form = data;
            this.modalVisible = true;
        },
        edit(v) {
            this.modalType = 1;
            this.modalTitle = '编辑任务';
            this.$refs.form.resetFields();
            // 转换null为""
            for (let attr in v) {
                if (v[attr] == null) {
                    v[attr] = '';
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);
            this.form = data;
            this.modalVisible = true;
        },
        pause(v) {
            this.$Modal.confirm({
                title: '确认停止',
                content: '您确认要停止任务 ' + v.jobClassName + ' ?',
                loading: true,
                onOk: () => {
                    pauseQuartz(v).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.getDataList();
                        }
                    });
                }
            });
        },
        resume(v) {
            this.$Modal.confirm({
                title: '确认恢复',
                content: '您确认要恢复任务 ' + v.jobClassName + ' ?',
                loading: true,
                onOk: () => {
                    resumeQuartz(v).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.getDataList();
                        }
                    });
                }
            });
        },
        doOnce(v) {
            this.$Modal.confirm({
                title: '执行一次',
                content: '您确认要执行一次该任务 ' + v.jobClassName + ' ?',
                loading: true,
                onOk: () => {
                    doOnce(v).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.getDataList();
                        }
                    });
                }
            });
        },
        remove(v) {
            this.$Modal.confirm({
                title: '确认删除',
                content: '您确认要删除任务 ' + v.jobClassName + ' ?',
                loading: true,
                onOk: () => {
                    deleteQuartz({ ids: v.id }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        },
        clearSelectAll() {
            this.$refs.table.selectAll(false);
        },
        changeSelect(e) {
            this.selectList = e;
        },
        delAll() {
            if (this.selectList.length <= 0) {
                this.$Message.warning('您还未选择要删除的数据');
                return;
            }
            this.$Modal.confirm({
                title: '确认删除',
                content: '您确认要删除所选的 ' + this.selectList.length + ' 条数据?',
                loading: true,
                onOk: () => {
                    let ids = '';
                    this.selectList.forEach(function (e) {
                        ids += e.id + ',';
                    });
                    ids = ids.substring(0, ids.length - 1);
                    deleteQuartz({ ids: ids }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('删除成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        },
        onRestWindow() {
            this.columns = this.restTableCellWidth(this.columns_1)
            this.labelWidth100 = this.searchLabelWidth(this.labelWidth100)
        }
    },
    mounted() {
        this.onRestWindow()
        window.addEventListener('resize',this.onRestWindow)
        this.init();
    },
    beforeDestroy() {
        window.removeEventListener('resize',this.onRestWindow)
    }
};
</script>
